﻿namespace GoodAdmin.Web.Core;

/// <summary>
/// 人员管理控制器
/// </summary>
[ApiDescriptionSettings("System", Tag = "人员管理")]
[Route("/system/user")]
[RoleDataScope]
public class UserController: IDynamicApiController
{
    private readonly ISysUserService _userService;
    private readonly IExtraOrgService _extraOrgService;
    private readonly IExtraUserService _extraUserService;

    public UserController(ISysUserService userService, IExtraOrgService extraOrgService, IExtraUserService extraUserService)
    {
        _userService = userService;
        _extraOrgService = extraOrgService;
        _extraUserService = extraUserService;
    }

    #region get查询操作
    /// <summary>
    /// 分页查询用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet("page")]
    [DisplayName("分页查询用户")]
    public async Task<SqlSugarPagedList<SysUser>> GetUserPage([FromQuery] UserPageInput input)
    {
        return await _extraUserService.Page(input);
    }
    /// <summary>
    /// 获取用户角色列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet("roleList")]
    [DisplayName("获取用户角色列表")]
    public async Task<List<RoleSelectorOutput>> GetUserRoleList([FromQuery] BaseIdInput input)
    {
        return await _userService.GetUserRoleList(input);
    }

    /// <summary>
    /// 获取组织列表树
    /// </summary>
    /// <returns></returns>
    [HttpGet("orgTree")]
    [DisplayName("获取组织列表树")]
    public async Task<List<SysOrg>> GetOrgTree()
    {
        return await _extraOrgService.GetOrgTree();
    }
    #endregion

    #region post
    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("add")]
    [DisplayName("添加用户")]

    public async Task AddUser(UserAddInput input)
    {
        await _extraUserService.AddUser(input);
    }
    /// <summary>
    /// 批量添加用户
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("batchImport")]
    [DisplayName("批量添加用户")]

    public async Task<ExcelImportResult> BatchImport(IFormFile file)
    {
        return await _userService.BatchAddUser(file);
    }
    /// <summary>
    /// 添加用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("update")]
    [DisplayName("添加用户")]
    public async Task UpdateUser(UserUpdateInput input)
    {
        await _extraUserService.UpdateUser(input);
    }

    /// <summary>
    /// 删除用户
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("delete")]
    [DisplayName("删除用户")]
    public async Task DeleteUser(List<BaseIdInput> input)
    {
        await _extraUserService.DeleteUser(input);
    }

    /// <summary>
    /// 改变用户状态
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("changeStatus")]
    [DisplayName("设置状态")]
    public async Task<bool> ChangeStatus(BaseIdInput input)
    {
        return  await _extraUserService.ChangeStatus(input);
    }
    /// <summary>
    /// 重置密码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("resetPassword")]
    [DisplayName("重置密码")]
    public async Task ResetPassword(BaseIdInput input)
    {
        await _extraUserService.ResetPassword(input);
    }

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("changePassword")]
    [DisplayName("修改密码")]
    public async Task ChangePassword(ChangePwdInput input)
    {
        await _extraUserService.ChangePassword(input);
    }
    /// <summary>
    /// 用户授权
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("grantRole")]
    [DisplayName("用户授权")]
    public async Task GrantUserRole(UserRoleInput input)
    {
        await _extraUserService.GrantUserRole(input);
    }

    /// <summary>
    /// 模板下载
    /// </summary>
    /// <returns></returns>
    [HttpGet("templateDownload")]
    [DisplayName("模板下载")]
    public FileStreamResult TemplateDownload()
    {
        return  _userService.TemplateDownload();
    }

    /// <summary>
    /// 错误报告下载
    /// </summary>
    /// <returns></returns>
    [HttpGet("errorDownload")]
    [DisplayName("错误报告下载")]
    public async Task<FileStreamResult> ErrorDownload([FromQuery] BaseIdInput input)
    {
        return await _userService.ErrorDownload(input);
    }
    #endregion
}